import { PLATFORM_FILM_RELEASE_TYPE_LOCAL, PLATFORM_FILM_RELEASE_TYPE_REMOTE } from "../constant/platform";
import { FilmMultimediaRelation } from "../entities/filmMultimediaRelation";

export class FilmReleaseService {
  constructor(opt) {
    this.requestFactory = opt.requestFactory;
    this.sequelize = opt.sequelize;
  }

  // 新增发布任务
  async addRelease(data){
    let { data: { result } } = await this.getReleaseDetail(data.id)

    result = result.filter(item => (item.job_type || PLATFORM_FILM_RELEASE_TYPE_REMOTE) == PLATFORM_FILM_RELEASE_TYPE_LOCAL);

    const transaction = await this.sequelize.transaction(); 
    
    try {
      for (const item of result) {
        await FilmMultimediaRelation.create({
          id: item.id,
          org_id: data.org_id,
          user_id: data.user_id,
          film_release_id: data.id,
          platform_account_user_id: item.platform_account_user_id,
          title: item.title,
          caption: item.caption,
          multimedia: {
            material_url: item.multimedia.material_url,
            cover_url: item.multimedia.cover_url,
          },
          hashtag_name: item.hashtag_name,
          platform: data.platform,
          visibility_type: item.visibility_type,
          download: item.download,
          anchor: item.anchor,
          associ_poi: item.associ_poi,
          job_time: item.job_time,
          extra_info: item.extra_info,
        }, { transaction })
      }

      await transaction.commit();
    } catch (error) {
      await transaction.rollback();
      throw error
    }
  }

  // 更新发布任务
  async updateRelease(data, id = null){
    let { data: { result } } = await this.getReleaseDetail(data.id)

    result = result.filter(item => (item.job_type || PLATFORM_FILM_RELEASE_TYPE_REMOTE) == PLATFORM_FILM_RELEASE_TYPE_LOCAL);
    if (id) {
      result = result.filter(item => item.id === id)
    }

    const transaction = await this.sequelize.transaction();

    try {
      for (const item of result) {
        const filmRelation = await FilmMultimediaRelation.findOne({
          where: {
            id: item.id
          }
        })

        if(! filmRelation) {
          await FilmMultimediaRelation.create({
            id: item.id,
            org_id: data.org_id,
            user_id: data.user_id,
            film_release_id: data.id,
            platform_account_user_id: item.platform_account_user_id,
            title: item.title,
            caption: item.caption,
            multimedia: {
              material_url: item.multimedia.material_url,
              cover_url: item.multimedia.cover_url,
            },
            hashtag_name: item.hashtag_name,
            platform: data.platform,
            visibility_type: item.visibility_type,
            download: item.download,
            anchor: item.anchor,
            associ_poi: item.associ_poi,
            job_time: item.job_time,
            extra_info: item.extra_info
          }, { transaction })
        } else {
          await FilmMultimediaRelation.update({
            release_status: item.release_status,
            api_failed: item.api_failed,
            release_reason: item.release_reason,
            job_time: item.job_time,
            extra_info: item.extra_info
          }, {
            where: { id: item.id },
            transaction
          })
        }
      }

      await transaction.commit();
    } catch (error) {
      await transaction.rollback();
      throw error
    }
  }

  // 修改发布状态
  async updateFilmMultimediaRelationStatus(id, data) {
    const transaction = await this.sequelize.transaction();

    try {
      const updateData = {
        release_status: data.release_status,
        api_failed: data.api_failed || '',
        release_reason: data.release_reason || '',
      };

      if (data.release_reason) {
        updateData.release_reason = data.release_reason.trim();
      }

      if (data.job_time) {
        updateData.job_time = data.job_time;
      }

      if (data.api_result) {
        updateData.api_result = data.api_result;
      }

      if (data.release_time) {
        updateData.release_time = data.release_time;
      }

      await FilmMultimediaRelation.update(updateData, {
        where: { id },
        transaction
      });

      console.log('updateFilmMultimediaRelationStatus', id, updateData)

      // Todo. 更改远端的发布状态
      await this.requestFactory.request({
        url: '/work_ai/user/filmrelease/update',
        method: 'PUT',
        data: {
          id,
          ...updateData
        }
      })

      await transaction.commit();
    } catch (error) {
      await transaction.rollback();

      throw error
    }
  }

  // 获取发布信息
  async getReleaseInfo(id, pageSize = 20) {
    const { data: { list: { data: list } } } = await this.getReleaseList(1, pageSize)

    const result = list.find(item => item.id === id)

    if (!result) {
      throw new Error('未找到该发布任务')
    }

    return result
  }

  // 获取发布详情(账号发布列表)
  async getReleaseDetail(id) {
    return this.requestFactory.request({
        url: '/work_ai/user/filmrelease/details',
        method: 'get',
        params: {
            filmId: id
        }
    })
  }

  // 获取发布列表
  async getReleaseList(page = 1, pageSize = 20) {
    return this.requestFactory.request({
        url: '/work_ai/user/filmrelease/list',
        method: 'get',
        params: {
            page,
            pageSize
        }
    })
  }

  // 获取发布详情
  async getFilmMultiMediaDetail(id) {
    return this.requestFactory.request({
        url: '/work_ai/user/filmRelease/detail',
        method: 'get',
        params: {
            id
        }
    })
  }
}